简体   繁体   English

Ajax Post请求示例

[英]Ajax Post request example

I was wondering if anyone could help me with a simple ajax request example just so I can wrap my head around the whole idea. 我想知道是否有人可以通过一个简单的ajax请求示例来帮助我,以便我可以将想法全盘考虑。 I tried testing an ajax request to search the word "rails" on github. 我尝试测试ajax请求以搜索github上的“ rails”一词。 So my code looks something like this: 所以我的代码看起来像这样:

$.ajax({
url: 'www.github.com',
type: 'post',
contentType: 'application/x-www-form-urlencoded',
data: {
    q: 'rails'
},
success: function(data) {
    console.log(data);
}

});

This request is responding with a 404 response. 该请求以404响应进行响应。 So, I'm just curious how are you supposed to know what the key names for the "data" element should be? 因此,我很好奇您应该如何知道“数据”元素的键名应该是什么? When I inspected the search bar on github, it told me the name of the element was q. 当我检查github上的搜索栏时,它告诉我元素的名称为q。 Hence why I used the key "q" and I wanted to search for say "rails". 因此,为什么我使用键“ q”,而我想搜索“ rails”。 If anyone could help me with this example or perhaps provide a better one that would be greatly appreciated. 如果有人可以帮助我解决这个例子,或者提供更好的例子,将不胜感激。 Thanks! 谢谢!

Try to add http in your url, but, for security reason you can't do Ajax Crossdomain request without autorisation of the github.com domain in your case. 尝试在您的url中添加http,但是出于安全原因,如果没有对github.com域进行自动保护,则无法执行Ajax跨域请求。

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM