user411103
user411103

Reputation:

Making HTTP GET request from Chrome Extension

I have built a chrome extension and, at time of loading, I need to make a GET request to a server (trying it now from localhost).

Using Chrome debugger I can see that the call is made, but it is never received on the locahost server (which is running).

Javascript code:

$.get( "http://localhost:8080/myproject/api/getInfo", { userId: "me" } )
  .done(function( data ) {
    alert('Received!');
});

This is what I can see from Chrome debugger:

Request URL:http://localhost:8080/myproject/api/getInfo?userId=me
Request Headers
Provisional headers are shown
Accept:*/*
Origin:chrome-extension://ginkmpnhbepolafnopjackbgefh
Query String Parameters
view source
view URL encoded
userId:me

If I put http://localhost:8080/myproject/api/getInfo?userId=me directly on a browser it works well.

What is wrong?

Upvotes: 2

Views: 2129

Answers (2)

user411103
user411103

Reputation:

The problem I had was that the browser was blocking calls from the HTTPS site (where the plugin was displayed) to HTTP://localhost.

It started working when I deployed it to production with a HTTPS server URL.

Upvotes: 0

Divyesh Kanzariya
Divyesh Kanzariya

Reputation: 3789

I don't know what is problem in above code but same things I have done using below code via creating an AJAX XMLHttpRequest Object on chrome extension script.

var userid="me";
var xmlHttpRequest = (window.XMLHttpRequest) ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlHttpRequest.open("GET","http://localhost:8080/myproject/api/getInfo",true);
xmlHttpRequest.send("userid="+userid);
xmlHttpRequest.onreadystatechange = function()
{
	if(xmlHttpRequest.readyState == XMLHttpRequest.DONE)
	{
		alert('Received!');
	}
}

Upvotes: 1

Related Questions